/**
 * @author  zhanshi Liu
 * @date    2020/04/06
 * @website www.jhmsona.cn
 */
let vm = new Vue({
    el:'#vm',
    data:{
        dictList:undefined,
        tableData:[],
        currentPage:1,
        count:0,
        title:undefined,
        statusOptions:[],
        dialogFormVisible: false,
        formLabelWidth: '80px',
        dictTypeStatus:'sys_status',
        respForm:{},
        menuTree:[],
        roleIds:undefined,
        form:{
            roleName:undefined,
            status:undefined,
            page:1,
            limit:10,
        },
        defaultProps: {
            children: 'children',
            label: 'name'
        },
        rules: {
            roleName: [{ required: true, message: '请输入角色名', trigger: 'blur' }]
            ,status: [{ required: true, message: '请选择状态', trigger: 'blur' }]

        }
    },
    created:function (){
        this.getDictList();
    },
    mounted:function(){
        this.getStatus();
    },
    methods:{
        getDictList(){
            axios.get('/sys/dict/getDictList').then((res)=> {
                this.dictList = res.data;
                axios.get('/sys/role/list', {
                    params: {
                        data:JSON.stringify(this.form)
                    }
                }).then(res => {
                    res.data.data.forEach(item => {
                        for( let key in this.dictList ){
                            if (this.dictTypeStatus+"_"+item.status == key){
                                item.status=this.dictList[key];
                            }
                        }
                    })
                    this.tableData=res.data.data;
                    this.count = res.data.count;
                }).catch(res=> {
                    console.log(res);
                });
            });
        },
        getMenuTree(){
            axios.get('/sys/menu/listNormal').then((res)=> {
                this.menuTree = res.data.data;
                console.log(this.menuTree.length)
            });
        },
        getCheckedNodes(){//获取选中节点
            let menuData = this.$refs.tree.getCheckedNodes(false,true);
            let menuIds = [];
            menuData.forEach((item) => {
                menuIds.push(item.menuId)
            })
            return menuIds;
        },
        getStatus(){
            axios.get('/sys/dict/getDictByType',{
                params:{
                    dictType:this.dictTypeStatus
                }
            }).then(res=> {
                this.statusOptions = res.data.data;
            })
        },
        resetForm(){
            this.respForm={
                roleId:undefined,
                roleName: undefined,
                status: '0',
                remark: undefined,
                menuIds: [],
            }
        },
        edit(index,row){//编辑
            this.dialogFormVisible = true;
            this.resetForm();
            this.getMenuTree();
            this.title="编辑角色";
            axios.get('/sys/role/info',{
                params:{
                    roleId:row.roleId
                }
            }).then(res=> {
                this.respForm = res.data.role;
                this.respForm.menuIds = res.data.menuIds;//已有菜单Ids
            })
        },
        selectionChange(val){//多选
            this.roleIds = val.map(item => item.roleId);
        },
        search(){
            this.getDictList();
        },
        reset(form1){
            this.$refs[form1].resetFields()
        },
        handleSizeChange(val) {
            this.form.limit = val;
            this.getDictList();
        },
        handleCurrentChange(val) {
            this.form.page = val;
            this.getDictList();
        },
        statusChange(){
            this.getDictList();
        },
        add(){//添加
            this.resetForm();
            this.dialogFormVisible = true;
            this.title="添加角色";
            this.getMenuTree();
        },
        remove(row){
            let ids = row.roleId || this.roleIds;//等价与if(row.userId!=null ||row.userId!=""){let ids = row.userId}else if......
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                $.ajax({
                    url:'/sys/role/del',
                    dataType:'json',
                    traditional:true,
                    type:'delete',
                    data:{
                        roleIds:ids
                    },
                    success:res=>{
                        if (res.code===200){
                            this.$notify({title:'成功',message:res.msg,type:'success'});
                            this.getDictList();
                        } else{
                            this.$notify({title:'警告',message:res.msg,type:'warning'});
                        }
                    }
                })
            }).catch(() => {
                this.$notify({type: 'info', message: '已取消删除',title:"提示"});
            });

        },
        submitForm(val){//提交
            this.respForm.menuIds = this.getCheckedNodes();//获取选中的所有节点
            this.$refs[val].validate(valid => {
                if(valid){
                    if(this.respForm.roleId === undefined){
                        $.ajax({
                            url:'/sys/role/add',
                            dataType:'json',
                            type: 'post',
                            data:{
                                data:JSON.stringify(this.respForm)
                            },
                            success:res=>{
                                if(res.code===200){
                                    this.$notify({ title: '成功',message: res.msg,type: 'success' });
                                    this.dialogFormVisible = false;
                                    this.getDictList();
                                }else{
                                    this.$notify({title: '警告',message:res.msg,type: 'warning'});
                                }
                            }
                        })
                    }else{
                       $.ajax({
                           url:'/sys/role/modify',
                           dataType: 'json',
                           type:'put',
                           data:{
                               data:JSON.stringify(this.respForm)
                           },
                           success:res=>{
                               if(res.code===200){
                                   this.$notify({ title: '成功',message: res.msg,type: 'success' });
                                   this.dialogFormVisible = false;
                                   this.getDictList();
                               }else{
                                   this.$notify({title: '警告',message:res.msg,type: 'warning'});
                               }
                           }
                       })
                    }
                }
            });
        }
    }
})

